from pandas import read_excel, DataFrame


def writeXlsx(self, formPath, clientPath, forwardColor, returnColor):
    df = loadMaplink(formPath, forwardColor, returnColor)
    df.to_excel(f"{clientPath}/地图标识导出.xlsx")
    self.text_tips.insert('0.0', "【地图标识】转文件成功：" + f"{clientPath}/地图标识导出.xlsx" + '\n')
    


def loadMaplink(filepath, forwardColor, returnColor):
    # 读取 Excel 文件
    df = read_excel(filepath)

    # 用于存储处理后的数据
    data_list = []

    # 遍历行
    for i, row in df.iterrows():
        # 处理 `PosiFrom` 数据
        from_coords = row["PosiFrom"].split("#")
        from_desc = {
            "mapid": row["MapFrom"],
            "X": from_coords[0],
            "Y": from_coords[-1],
            "sShowName": row["MapTo"],
            "nColor": forwardColor,
            "bOutLine": 1,
        }
        data_list.append(from_desc)

        # 如果是双向连接，处理 `PosiTo` 数据
        if row["TwoWay"] == 1:
            to_coords = row["PosiTo"].split("#")
            to_desc = {
                "mapid": row["MapTo"],
                "X": to_coords[0],
                "Y": to_coords[-1],
                "sShowName": row["MapFrom"],
                "nColor": returnColor,
                "bOutLine": 1,
            }
            data_list.append(to_desc)

    # 将列表转换为 DataFrame
    data = DataFrame(data_list)
    return data


if __name__ == "__main__":
    desc = loadMaplink("cfg_maplink.xls")
    desc.to_excel("cfg_desc.xlsx")
